package com.example.tms.mapper;


import com.example.tms.bean.RechargeInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface RechargeMapper extends JpaRepository<RechargeInfo,String> {



    //查询充值信息（油卡id）
    List<RechargeInfo> findByYkidAndSfczOrderByCznumAsc(String Ykid, boolean sfcz);

    //查询充值信息数（油卡id）
    @Query(value = "select count(*) from recharge_info where ykid = ?1 and sfcz = true", nativeQuery = true)
    int findCountByYkidAndSfcz(String ykid);

    //查询所有充值信息
    @Query(value = "select * from recharge_info  where sfcz = true", nativeQuery = true)
    List<RechargeInfo>  findAll();

    //删除充值部分充值信息上移
    @Query(value = "update recharge_info set cznum = cznum-1 where ykid = ?1 and  cznum>?2", nativeQuery = true)
    @Modifying
    @Transactional
    void updatePart(String ykid, int cznum);

    //删除充值信息（假删除 充值id）
    @Query(value = "update recharge_info set cznum = 0, sfcz = false where czid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void DeleteByCzid(String czid);

    //查询区域信息（充值id）
    RechargeInfo findByCzidAndSfcz(String czid, boolean sfcz);

    //查询区域信息（油卡id，充值）
    RechargeInfo findByYkidAndCznum(String ykid, int cznum);

    //计算累计充值金额
    @Query(value = "select sum(czje) FROM recharge_info where ykid = ?1 and sfcz = true", nativeQuery = true)
    String sumCzje(String ykid);

    RechargeInfo findByYkid(String ykid);
}
